package Dao;

import Bean.CarDetail;
import Utils.DBUtils;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CarDao {

    //获取所有汽车信息方法
    public static ArrayList<CarDetail> getAllCar() {
        //创建一个集合用来存放汽车对象
        ArrayList<CarDetail> list = new ArrayList<>();
        String sql = "select * from brand b left join cardetail c on b.BrandID=c.BrandID;";
        ResultSet rs = DBUtils.query(sql);
        try {
            while (rs.next()) {
                int cid = rs.getInt("CID");
                String cname = rs.getString("Cname");
                String content = rs.getString("Content");
                Date lTime = rs.getDate("LTime");
                int price = rs.getInt("price");
                int brandID = rs.getInt("BrandID");
                String brandName = rs.getString("BrandName");
                //将获取的数据封装成一个对象并添加到集合
                CarDetail car = new CarDetail(cid, cname, content, lTime, price,brandName);
                list.add(car);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    //添加汽车方法
    public static int addCar(CarDetail car){
        //编写sql语句
        String sql = "insert into cardetail values(?,?,?,?,?,?)";
        //将获取的对象拆封赋值给每个?
        int i = DBUtils.update(sql, car.getCid(), car.getCName(), car.getContent(), car.getLTime(), car.getPrice(), car.getBrandID());
        return i;
    }
}
